package com.qifeng.lab.mt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.qifeng.common.utils.DesensitizedUtil;
import com.qifeng.common.utils.ObjUtils;
import com.qifeng.lab.base.utils.LabBase;
import io.netty.util.internal.ObjectUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qifeng.common.annotation.Log;
import com.qifeng.common.core.controller.BaseController;
import com.qifeng.common.core.domain.AjaxResult;
import com.qifeng.common.enums.BusinessType;
import com.qifeng.common.utils.StringUtils;
import com.qifeng.common.constant.UserConstants;
import com.qifeng.lab.mt.domain.LabMtForm;
import com.qifeng.lab.mt.service.ILabMtFormService;
import com.qifeng.common.utils.poi.ExcelUtil;
import com.qifeng.common.core.page.TableDataInfo;


/**
 * 计量单据Controller
 * @author qifeng
 */
@RestController
@RequestMapping("/labmt/form")
public class LabMtFormController extends BaseController
{
    @Autowired
    private ILabMtFormService labMtFormService;



    /**
     * 查询计量单据列表
     */
    @PreAuthorize("@ss.hasPermi('labmt:form:list')")
    @GetMapping("/list")
    public TableDataInfo list(LabMtForm labMtForm)
    {

        startPage();
        List<LabMtForm> list = labMtFormService.selectLabMtFormList(labMtForm);
        return getDataTable(list);
    }

    /**
     * 导出计量单据列表
     */
    @PreAuthorize("@ss.hasPermi('labmt:form:export')")
    @Log(title = "计量单据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LabMtForm labMtForm)
    {
        List<LabMtForm> list = labMtFormService.selectLabMtFormList(labMtForm);
        ExcelUtil<LabMtForm> util = new ExcelUtil<LabMtForm>(LabMtForm.class);
        util.exportExcel(response, list, "计量单据数据");
    }


    /**
     * 获取计量单据详细信息
     */
    @PreAuthorize("@ss.hasPermi('labmt:form:query')")
    @GetMapping(value = "/{formId}")
    public AjaxResult getInfo(@PathVariable("formId") Long formId)
    {
        LabMtForm labMtForm = labMtFormService.selectLabMtFormByFormId(formId);

        Map map = new HashMap<>();
        map.put("company", LabBase.getCom());
        labMtForm.setParams(map);
        //labMtForm.setFormSendUserPhone(DesensitizedUtil.phone(labMtForm.getFormSendUserPhone()));
        return AjaxResult.success(labMtForm);
    }

    /**
     * 新增计量单据
     */
    @PreAuthorize("@ss.hasPermi('labmt:form:add')")
    @Log(title = "计量单据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody LabMtForm labMtForm)
    {
        /* 重复检查 单据编号 */
        if (StringUtils.isNotEmpty(labMtForm.getFormNo())
                && UserConstants.NOT_UNIQUE.equals(labMtFormService.checkLabMtFormFormNo(labMtForm)))
        {
            return AjaxResult.error("操作失败，单据编号已存在");
        }

        //单据来源 = 手动输入
        labMtForm.setFormSource(0L);

        return toAjax(labMtFormService.insertLabMtForm(labMtForm));
    }

    /**
     * 修改计量单据
     */
    @PreAuthorize("@ss.hasPermi('labmt:form:edit')")
    @Log(title = "计量单据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody LabMtForm labMtForm)
    {
        /* 重复检查 单据编号 */
        if (StringUtils.isNotEmpty(labMtForm.getFormNo())
                && UserConstants.NOT_UNIQUE.equals(labMtFormService.checkLabMtFormFormNo(labMtForm)))
        {
            return AjaxResult.error("操作失败，单据编号已存在");
        }

        return toAjax(labMtFormService.updateLabMtForm(labMtForm));
    }




    /**
     * 删除计量单据
     */
    @PreAuthorize("@ss.hasPermi('labmt:form:remove')")
    @Log(title = "计量单据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{formIds}")
    public AjaxResult remove(@PathVariable Long[] formIds)
    {
        return toAjax(labMtFormService.deleteLabMtFormByFormIds(formIds));
    }


    /**
     * 发送完工短信
     */
    @PreAuthorize("@ss.hasPermi('labmt:form:smssend')")
    @Log(title = "计量单据", businessType = BusinessType.UPDATE)
    @PostMapping("/smssend/{formIds}")
    public AjaxResult smsSend(@PathVariable Long[] formIds)
    {
        return  labMtFormService.smsSend(formIds);
    }

}
